//
//  SSFirstPageWebView.m
//  Travel_World
//
//  Created by dlios on 15/10/13.
//  Copyright © 2015年 TLS. All rights reserved.
//

#import "SSFirstPageWebView.h"
#import "SSCollectionDataTool.h"
#import "SSCollectionModel.h"
#import "Masonry.h"

@interface SSFirstPageWebView ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *myWebView;

@end

@implementation SSFirstPageWebView

- (void)viewWillAppear:(BOOL)animated
{
    if (self.collectionModel) {
        
        [self styleOfRightBarButton];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    //self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.949 green:0.179 blue:1.000 alpha:1.000];
    self.myWebView = [[UIWebView alloc] init];
    self.myWebView.delegate = self;
    [self.view addSubview:self.myWebView];
    [self.myWebView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.and.right.top.bottom.equalTo(self.view);
        
    }];
    
    NSLog(@"%@", [NSURL URLWithString:self.myURL]);
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.myURL]];
    [self.myWebView loadRequest:request];
    
    if (self.collectionModel) {
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-shoucang"] style:UIBarButtonItemStyleDone target:self action:@selector(rightButton)];
    }
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        return NO;
    }
    
    return YES;
}


- (void)styleOfRightBarButton
{
    [[SSCollectionDataTool shareSSCollectionDataToolWithModelType:DiscountModel] createTabelWithModelType:DiscountModel];
    if (self.collectionModel) {
        
        if ([[SSCollectionDataTool shareSSCollectionDataToolWithModelType:DiscountModel] isIntable:self.collectionModel] == NotInTable) {
            self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"iconfont-shoucang"];
        } else {
            self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"iconfont-shoucangxuanzhong"];
        }
    }
}


- (void)rightButton
{

    if (self.collectionModel) {
        
        SelectInTable result = [[SSCollectionDataTool shareSSCollectionDataToolWithModelType:DiscountModel] isIntable:self.collectionModel];
        
        if (result == InTable) {
            
            [[SSCollectionDataTool shareSSCollectionDataToolWithModelType:DiscountModel] deleteCollectoinData:self.collectionModel modelType:DiscountModel];
            
            self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"iconfont-shoucang"];
            
        } else if (result == NotInTable) {
            
            [[SSCollectionDataTool shareSSCollectionDataToolWithModelType:DiscountModel] inserCollectionData:self.collectionModel];
            self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"iconfont-shoucangxuanzhong"];
            
        } else {
            
            NSLog(@"操作失败");
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
